package com.shop.cloud.service.hongfu.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.shop.cloud.common.mybatis.BaseService;
import com.shop.cloud.common.mybatis.MybatisDao;
import com.shop.cloud.dao.hongfu.UserDao;
import com.shop.cloud.domain.hongfu.BalanceHistory;
import com.shop.cloud.domain.hongfu.PointsHistory;
import com.shop.cloud.domain.hongfu.User;
import com.shop.cloud.service.hongfu.IBalanceHistoryService;
import com.shop.cloud.service.hongfu.IPointsHistoryService;
import com.shop.cloud.service.hongfu.IUserService;

/**
 * user
 */
@Service("userService")
public class UserService extends BaseService<User> implements IUserService {

	@Autowired
	private UserDao userDao;
	
	@Autowired
    private IBalanceHistoryService balanceHistoryService;
	
	@Autowired
	private IPointsHistoryService pointsHistoryService;

	@Override
	public MybatisDao<User> getEntityDao() {
		return userDao;
	}

	public User findByIdOrPhone(final Map<String, Object> params) {
		return userDao.findByIdOrPhone(params);
	}

	public List<User> finddown(User user) {
		// TODO Auto-generated method stub
		// 二级
		List<User> alllist = userDao.finddown(user);
		if (alllist != null && alllist.size() > 0) {
			for (User user2 : alllist) {
				// 三级
				List<User> seclist = userDao.finddown(user2);
				user2.setSecondUsers(seclist);
			}
		} 

		Gson gson = new Gson();
		System.out.println(gson.toJson(alllist));
		return alllist;
	}

	@Override
	public User topUser(User user) {

		return userDao.topUser(user);
	}

	@Override
	public int handleUserGivingPoints(Long userId, User user) {
		
		String phone = user.getPhone();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("phone", phone);
		
		User insUser = userDao.selectMsgByPhone(params);
		if(null == insUser){
			//用户不存在
			return 0;
		}else{
			if(userId == insUser.getId()){
				//不能赠送自己
				return 3;
			}else{
				//获取要赠送积分
				int points = user.getPoints();
				User fromUser = this.get(userId);
				int fromPoints = fromUser.getPoints();
				if(fromPoints < points){
					//赠送积分者本身所拥有的积分不足
					return 1;
				}else{
					fromUser.setPoints(fromPoints - points);
					this.update(fromUser);
					//关联交易流水号
					String tradeNo_f = DateFormatUtils.format(new Date(), "yyyyMMddHHmmss") + ((int)((Math.random()*9+1)*100000));
					
					BalanceHistory balanceHistory = new BalanceHistory();
					balanceHistory.setUserId(userId);
					balanceHistory.setType(23);
					balanceHistory.setCredits(points);
					balanceHistory.setOrderNo(tradeNo_f);
					balanceHistory.setCreateTime(new Timestamp((new Date()).getTime()));
					balanceHistory.setRemark(fromUser.getNickname()+"赠送"+insUser.getNickname()+points+"积分");
					//创建流水
					balanceHistoryService.insert(balanceHistory);
					
					PointsHistory pointsHistory = new PointsHistory();
					pointsHistory.setUserId(userId);
					pointsHistory.setType(8);
					pointsHistory.setCurrentPoints(fromUser.getPoints());
					pointsHistory.setChangePoints(points);
					pointsHistory.setCreateTime(new Timestamp((new Date()).getTime()));
					pointsHistory.setPointNo(tradeNo_f);
					pointsHistoryService.insert(pointsHistory);
					
					insUser.setPoints(insUser.getPoints() + points);
					this.update(insUser);
					//关联交易流水号
					String tradeNo_t = DateFormatUtils.format(new Date(), "yyyyMMddHHmmss") + ((int)((Math.random()*9+1)*100000));
					
					BalanceHistory balanceHistory_0 = new BalanceHistory();
					balanceHistory_0.setUserId(insUser.getId());
					balanceHistory_0.setType(22);
					balanceHistory_0.setCredits(points);
					balanceHistory_0.setOrderNo(tradeNo_t);
					balanceHistory_0.setCreateTime(new Timestamp((new Date()).getTime()));
					balanceHistory_0.setRemark(insUser.getNickname()+"获得"+fromUser.getNickname()+"赠送的"+points+"积分");
					//创建流水
					balanceHistoryService.insert(balanceHistory_0);
					
					PointsHistory pointsHistory_0 = new PointsHistory();
					pointsHistory_0.setUserId(insUser.getId());
					pointsHistory_0.setType(9);
					pointsHistory_0.setCurrentPoints(insUser.getPoints());
					pointsHistory_0.setChangePoints(points);
					pointsHistory_0.setCreateTime(new Timestamp((new Date()).getTime()));
					pointsHistory_0.setPointNo(tradeNo_t);
					pointsHistoryService.insert(pointsHistory_0);
				}
			}
		}
			
		//操作成功
		return 2;
	}
}